package com.interagriculture.controller;

import com.interagriculture.annotation.AdminVerify;
import com.interagriculture.common.CustomException;
import com.interagriculture.common.Result;
import com.interagriculture.entity.Data;
import com.interagriculture.enums.ExceptionCodeEnum;
import com.interagriculture.service.DataService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;

/**
 * @author 11 2024/11/12
 */
@RestController
@RequestMapping("/adminInfo")
public class AdminDataController {

    @Resource
    private DataService dataService;

    /**
     * 新增或修改
     * @param data
     * @return
     */
    @PostMapping("/saveOrUpdate")
    @AdminVerify
    public Result saveOrUpdate(@RequestBody Data data){
           dataService.saveOrUpdate(data);
           return Result.ok();

    }

    @GetMapping("/{dataId}")
    @AdminVerify
    public Result<Data> getById(@PathVariable @NotNull Integer dataId){
        Data data = dataService.queryById(dataId);
        return Result.ok(data);
    }
    @PostMapping("/delete")
    @AdminVerify
    public Result deleteById(@RequestParam @NotNull Integer dataId){
        boolean deleteRes = dataService.deleteById(dataId);
        if(!deleteRes){
            throw new CustomException(ExceptionCodeEnum.DELETE_ERROR);
        }

        return Result.ok();
    }
}
